package org.maiyoframework.core.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * 基础工具类
 * 
 * @author maiYo
 * 
 */
public class Utils {

	/**
	 * 获取编码后的请求路径
	 * 
	 * @param req
	 *            request请求
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getEncodeRequestUrl(HttpServletRequest req) {
		String url = req.getRequestURL().toString();
		String query = req.getQueryString();
		if (query != null && query.length() > 0) {
			url += "?" + query;
		}
		url = encodeUrl(url);
		return url;
	}

	/**
	 * 按UTF-8编码URL
	 * 
	 * @param url
	 *            地址
	 * @return
	 */
	public static String encodeUrl(String url) {
		try {
			if (StringUtils.isNotBlank(url)) {
				url = URLEncoder.encode(url, "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return url;
	}

	/**
	 * 按UTF-8解码URL
	 * 
	 * @param url
	 *            地址
	 * @return
	 */
	public static String decodeUrl(String url) {
		try {
			if (StringUtils.isNotBlank(url)) {
				url = URLDecoder.decode(url, "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return url;
	}

	/**
	 * 解码URL同时干掉项目根路径
	 * 
	 * @param url
	 *            地址
	 * @param req
	 *            request请求
	 * @return
	 */
	public static String decodeUrlIgnoreContextPath(String url,
			HttpServletRequest req) {
		String root = appRoot(req);
		url = decodeUrl(url);
		if (url.startsWith(root)) {
			url = url.substring(root.length());
		}
		return url;
	}

	/**
	 * 获取应用根路径
	 * 
	 * @param req
	 *            request请求
	 * @return
	 */
	public static String appRoot(HttpServletRequest req) {
		String port = ":" + req.getServerPort();
		// 过虑掉默认80端口
		if (port.equals(":80")) {
			port = port.replace(":80", "");
		}
		String root = req.getScheme() + "://" + req.getServerName() + port
				+ req.getContextPath();
		return root;
	}

	/**
	 * 字符时间转换为指定形式的Date类型时间
	 * 
	 * @param dateTime
	 *            字符时间
	 * @param pattern
	 *            格式形式
	 * @return
	 */
	public static Date getDateTime(String dateTime, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		Date date = null;
		try {
			date = sdf.parse(dateTime);
		} catch (Exception ex) {

		}
		return date;
	}

	/**
	 * Date类型时间转换为指定形式的字符时间
	 * 
	 * @param dateTime
	 *            Date类型时间
	 * @param pattern
	 *            格式形式
	 * @return
	 */
	public static String getDateTime(Date dateTime, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		return sdf.format(dateTime);
	}

	/**
	 * 获取指定日期开始时间
	 * 
	 * @param date
	 *            字符日期
	 * @return
	 */
	public static Date getStartDateTime(String date) {
		date += " 00:00:00";
		return Utils.getDateTime(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 获取指定日期结束时间
	 * 
	 * @param date
	 *            字符日期
	 * @return
	 */
	public static Date getEndDateTime(String date) {
		date += " 23:59:59";
		return Utils.getDateTime(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 获取当前时间戳，精确到'yyyyMMddHHmmssSSS'
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		Date date = new Date();
		return getDateTime(date, "yyyyMMddHHmmssSSS");
	}

	/**
	 * 获取UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString();
	}
}
